package learn;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class FullArray {
    // 存放符合条件结果的集合
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    // 用来存放符合条件结果
    LinkedList<Integer> path = new LinkedList<Integer>();

    boolean[] used;

    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0){
            return result;
        }
        used = new boolean[nums.length];
        permuteHelper(nums);
        return result;
    }


    private void permuteHelper(int[] nums){
        if (nums.length == path.size()){
            result.add(new ArrayList<Integer>(path));
            return;
        }

        //处理每个结点,使用used[]=ture表示某个数据使用过了
        for (int i=0;i<nums.length;i++){
            if (used[i]){
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            permuteHelper(nums);
            path.removeLast();
            used[i] = false;
        }
    }

    public static void main(String[] args) {
        FullArray solution = new FullArray();
        int [] nums = {1,2,3};
        solution.permute(nums);
        System.out.println(JSONObject.toJSON(solution.result));
    }

}














